package com.gx.recruitment.controller;

import com.gx.recruitment.entity.User_info;
import com.gx.recruitment.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/user/add")
    public Map<String,Object> registerUser(String username, String userpass, String useremail) {
        Map<String,Object> map =new HashMap<>();
        User_info user_info = new User_info();
        user_info.setUserName(username);
        user_info.setUserPass(userpass);
        user_info.setUserEmail(useremail);
        List<User_info> users = userService.findbyUserName(username);
        if(users != null && users.size() > 0){
            map.put("msg","用户名已经存在");
            return map;
        }else {
            int res = userService.insertUser(user_info);
            if (res == 0) {
                map.put("msg","success");
                return map;
            } else {
                map.put("msg","fail");
                return map;
            }
        }
    }

    @PostMapping("/user/login")
    public Map<String,Object> loginUser(String username,String userpass){
        Map<String,Object> map =new HashMap<>();
        List<User_info> users = userService.loginUser(username,userpass);
        System.out.println(users.get(0).getUserId());
        if (users != null && users.size() > 0) {
            map.put("msg","success");
            map.put("userid",users.get(0).getUserId());
            return map;
        }else {
            map.put("msg","fail");
            return map;
        }
    }

}
